package com.bit.strongbaby;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@SuppressLint("NewApi")
public class InsertActivity extends Activity {
	EditText ed1;
	EditText ed2;
	Button btn1;
	Button btn2;
	@TargetApi(Build.VERSION_CODES.GINGERBREAD)
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_insert);

		ed1 = (EditText)findViewById(R.id.editText1);
		ed2 = (EditText)findViewById(R.id.editText2);
		btn1 = (Button)findViewById(R.id.tbutton1);
		btn1.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent ini = new Intent(getBaseContext(), SelectActivity.class);
				startActivity(ini);
				String msg = ed1.getText().toString();
				String msg1 = ed2.getText().toString();
				try {
					if(android.os.Build.VERSION.SDK_INT>9){
						StrictMode.ThreadPolicy polic = new StrictMode.ThreadPolicy.Builder()
														.permitAll().build();
						StrictMode.setThreadPolicy(polic);
					}

					String URL = "http://192.168.0.182:8080/ServerApp/JSONServer.jsp";
					
					HttpClient client = new DefaultHttpClient();
					
					HttpParams params = client.getParams();
					HttpConnectionParams.setConnectionTimeout(params, 3000);
					HttpConnectionParams.setSoTimeout(params, 3000);
					
					List par = new ArrayList();
					par.add(new BasicNameValuePair("msg", msg));
					par.add(new BasicNameValuePair("msg1", msg1));
					UrlEncodedFormEntity ent = new UrlEncodedFormEntity(par, "euc-kr");
					HttpPost post = new HttpPost(URL);
					post.setEntity(ent);
					HttpResponse respost = client.execute(post);
					HttpEntity reent = respost.getEntity();
					finish();
					//client.execute(post);
				} catch (ClientProtocolException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				//String ed1_1 = ed1.getText().toString();
				//String ed2_1 = ed2.getText().toString();
				//String result = SendByHttp(ed1_1);
				//String[][] paresedDate = jsonParserList(result);
				
				//txt1.setText(result);
				
			}
		});
		btn2 = (Button)findViewById(R.id.tbutton2);
		btn2.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent ini = new Intent(getBaseContext(), SelectActivity.class);
				startActivity(ini);
				finish();
				
			}
		});
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
